﻿using Microsoft.Win32;

namespace BM.Common
{
    /// <summary>
    /// This class contain Bluestack infomation
    /// </summary>
    public class BSConfig
    {
        /// <summary>
        /// Install directory of Bluestack. Default is C:\Program Files(x86)\BlueStacks ...
        /// </summary>
        public string InstallDir { get; set; }
        /// <summary>
        /// Data directory of Bluestack. Default is C:\ProgramData\BlueStacks
        /// </summary>
        public string DataDir { get; set; }
        /// <summary>
        /// Bluestack window's width
        /// </summary>
        public int WindowWidth { get; set; }
        /// <summary>
        /// Bluestack window's height
        /// </summary>
        public int WindowHeight { get; set; }
        /// <summary>
        /// Bluestack memory RAM
        /// </summary>
        public int PhysicMemory { get; set; }

        /// <summary>
        /// Init new instance of BS Config
        /// </summary>
        private BSConfig()
        { }

        /// <summary>
        /// Load setting of BS Config
        /// </summary>
        /// <returns></returns>
        public static BSConfig LoadSetting()
        {
            try
            {
                var bs = new BSConfig();

                var bsKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\BlueStacks");
                var frame = bsKey.OpenSubKey("Guests\\Android\\FrameBuffer\\0");

                bs.InstallDir = bsKey.GetValue("InstallDir").ToString();
                bs.DataDir = bsKey.GetValue("DataDir").ToString();

                // for v9.xx bsWindowHeight = frame.WindowHeight
                // for v8.xx bsWindowHeight = frame.Height
                // else bsWindowHeight = 600 for older version
                bs.WindowHeight = int.Parse(frame.GetValue("WindowHeight", frame.GetValue("Height", 600).ToString()).ToString());

                // for v9.xx bsWindowWidth = frame.WindowWidth
                // for v8.xx bsWindowWidth = frame.Width
                // else 960 for older version
                bs.WindowWidth = int.Parse(frame.GetValue("WindowWidth", frame.GetValue("Width", 960).ToString()).ToString());
                bs.PhysicMemory = int.Parse(bsKey.OpenSubKey("Guests\\Android").GetValue("Memory", 1024).ToString());

                frame.Close();
                bsKey.Close();

                return bs;
            }
            catch { throw; }
        }

        /// <summary>
        /// Save Bluesatck configuration
        /// </summary>
        /// <param name="bsConfig"></param>
        public static void SaveSetting(BSConfig bsConfig)
        {
            try
            {
                var android = Registry.LocalMachine.OpenSubKey("SOFTWARE\\BlueStacks\\Guests\\Android", true);
                // Set memory
                android.SetValue("Memory", bsConfig.PhysicMemory, RegistryValueKind.DWord);
                android.Close();

                var frame0 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\BlueStacks\\Guests\\Android\\FrameBuffer\\0", true);

                // Set screen size with version 9.xx
                frame0.SetValue("WindowHeight", bsConfig.WindowHeight, RegistryValueKind.DWord);
                frame0.SetValue("WindowWidth", bsConfig.WindowWidth, RegistryValueKind.DWord);

                // Set screen size with version 8.xx
                frame0.SetValue("Height", bsConfig.WindowHeight, RegistryValueKind.DWord);
                frame0.SetValue("Width", bsConfig.WindowWidth, RegistryValueKind.DWord);

                // close rk
                frame0.Close();
            }
            catch { throw; }
        }
    }
}
